package com.carlinfo.demo.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.DbUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IADynastyDao;
import com.carlinfo.demo.pojo.ADynasty;

public class ADynastyDaoImpl extends BaseDaoImpl<ADynasty> implements IADynastyDao
{
	public ADynastyDaoImpl()
	{
		ADynasty dyansty = new ADynasty();
		this.baseObj = dyansty;
	}
	@Override
	public int saveOneDao(ADynasty dynasty)
	{
		String sql = "insert into a_dynasty values (null,?,?,?,?,?,?,?,?,?)";
		List<Object> list = new ArrayList<Object>();
		list.add(dynasty.getName());
		list.add(dynasty.getCapital());
		list.add(dynasty.getAge());
		list.add(dynasty.getStatus());
		list.add(dynasty.getContext());
		list.add(dynasty.getSt());
		list.add(dynasty.getCreateTime());
		list.add(dynasty.getPubTime());
		list.add(dynasty.getUpdateTime());

		Map<String, Object> condMap = new HashMap<String, Object>();

		int res = this.excJDBC(sql, list, condMap);
		return res;
	}

	@Override
	public List<ADynasty> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from a_dynasty where 1=1 ";
		/*
		 * 关键字: select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%'); 状态 : select * from a_dynasty where status = 1 ; 时间 :
		 * select * from a_dynasty where (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 关键字+状态: select * from a_dynasty where (name like
		 * '%0%' or capital like '%0%' or context like '%0%') and status = 1 ; 关键字+时间:
		 * select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%') and (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 时间+状态: select * from a_dynasty where status = 1 and
		 * (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
		 * 关键字+状态+时间: select * from a_dynasty where (name like '%0%' or capital like
		 * '%0%' or context like '%0%') and status = 1 and (pubTime >= '2018-09-18
		 * 00:00:00' and pubTime < '2018-09-19 10:00:00'); 啥也不选: select * from
		 * a_dysnasty ;
		 */

		/* 根据条件来组装sql语句 */
		if (condMap.get("keyWord") != null && !"".equalsIgnoreCase(condMap.get("keyWord") + ""))
		{
			sql += " and (name like ? or capital like ? or context like ?) ";
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			list.add(condMap.get("status"));
		}
		if (condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			sql += " and  (pubTime >= ? and pubTime < ?) ";
			list.add(condMap.get("stDate"));
			list.add(condMap.get("edDate"));
		}

		/**
		 * 设置排序方式
		 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc ";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc,name desc ";
		} else
		{
			sql += " order by pubTime desc ";
		}

		if (pageInfoUtil != null)
		{
			return this.findListByPage(pageInfoUtil, sql, list);
		}
		return this.findList(sql, list);
	}

	/**
	 * 将数据库转换为pojo
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	protected ADynasty convertFromDb_source(ResultSet rs) throws SQLException
	{
		ADynasty dynasty = new ADynasty();
		dynasty.setAge(rs.getInt("age"));
		dynasty.setCapital(rs.getString("capital"));
		dynasty.setId(rs.getInt("id"));
		dynasty.setContext(rs.getString("context"));
		dynasty.setCreateTime(rs.getTimestamp("createTime"));
		dynasty.setName(rs.getString("name"));
		dynasty.setPubTime(rs.getTimestamp("pubTime"));
		dynasty.setStatus(rs.getByte("status"));
		dynasty.setUpdateTime(rs.getTimestamp("updateTime"));
		dynasty.setSt(rs.getTimestamp("st"));
		return dynasty;
	}

	@Override
	public ADynasty selectOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from a_dynasty ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ? limit 1;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("name") + "")) && condMap.get("name") != null)
		{
			sql += "where name = ? limit 1;";
			list.add(condMap.get("name"));
		}
		return this.findONeJDBC(sql, list);
	}

	@Override
	public int updateOneDao(ADynasty dynasty)
	{
		String sql = "update a_dynasty set name=?,capital=?,age=?,status=?,context=?"
				+ ",st=?,createTime=?,pubTime=?,updateTime=? where id =?;";
		List<Object> list = new ArrayList<Object>();
		list.add(dynasty.getName());
		list.add(dynasty.getCapital());
		list.add(dynasty.getAge());
		list.add(dynasty.getStatus());
		list.add(dynasty.getContext());
		list.add(dynasty.getSt());
		list.add(dynasty.getCreateTime());
		list.add(dynasty.getPubTime());
		list.add(dynasty.getUpdateTime());
		list.add(dynasty.getId());

		return this.excJDBC(sql, list);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "delete from a_dynasty ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ?;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("name") + "")) && condMap.get("name") != null)
		{
			sql += "where name = ? ;";
			list.add(condMap.get("name"));
		}
		return this.excJDBC(sql, list);
	}

}
